import java.util.Stack;

public class Solution5 {
    public static void main(String[] args) {
        int[] pushed = {1,2,3,4,5};
        int[] popped = {4,5,3,2,1};
        System.out.println(validateStackSequences(pushed, popped));
    }
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> st = new Stack<>();
        int i = 0;
        int n = popped.length;
        for(int j = 0; j < pushed.length; j++) {
            st.push(pushed[j]);
            while(!st.isEmpty() && i < n  && st.peek() == popped[i]) {
                st.pop();
                i++;
            }
        }
        if(st.isEmpty()) {
            return true;
        }
        return false;
    }
}
